﻿
 
using System;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Diagnostics.CodeAnalysis;
using GestionSaludOcupacional.Data;
using System.ComponentModel.Composition;

namespace GestionSaludOcupacional.Repository
{
	[Export(typeof(IUnitOfWork))]
	public partial class UnitOfWork : IUnitOfWork
	{
		private readonly HealthBookEntities _context;

		public virtual HealthBookEntities Context
        {
            get { return _context; }
        }

		private readonly Lazy<IRepository<Anamnesi>> _anamnesiRepository;
		private readonly Lazy<IRepository<AntecedenteEnfermedad>> _antecedenteEnfermedadRepository;
		private readonly Lazy<IRepository<CentroTrabajo>> _centroTrabajoRepository;
		private readonly Lazy<IRepository<Cita>> _citaRepository;
		private readonly Lazy<IRepository<ExamenAudicion>> _examenAudicionRepository;
		private readonly Lazy<IRepository<ExamenFisico>> _examenFisicoRepository;
		private readonly Lazy<IRepository<ExamenPsicologia>> _examenPsicologiaRepository;
		private readonly Lazy<IRepository<ExamenPsicosensometria>> _examenPsicosensometriaRepository;
		private readonly Lazy<IRepository<ExamenVision>> _examenVisionRepository;
		private readonly Lazy<IRepository<FactorRiesgo>> _factorRiesgoRepository;
		private readonly Lazy<IRepository<HistoriaClinica>> _historiaClinicaRepository;
		private readonly Lazy<IRepository<HistoriaClinicaRegistro>> _historiaClinicaRegistroRepository;
		private readonly Lazy<IRepository<Individuo>> _individuoRepository;
		private readonly Lazy<IRepository<IndividuoBlob>> _individuoBlobRepository;
		private readonly Lazy<IRepository<Log>> _logRepository;
		private readonly Lazy<IRepository<Medico>> _medicoRepository;

        public virtual IRepository<Anamnesi> AnamnesiRepository
        {
            get { return _anamnesiRepository.Value; }
        }

        public virtual IRepository<AntecedenteEnfermedad> AntecedenteEnfermedadRepository
        {
            get { return _antecedenteEnfermedadRepository.Value; }
        }

        public virtual IRepository<CentroTrabajo> CentroTrabajoRepository
        {
            get { return _centroTrabajoRepository.Value; }
        }

        public virtual IRepository<Cita> CitaRepository
        {
            get { return _citaRepository.Value; }
        }

        public virtual IRepository<ExamenAudicion> ExamenAudicionRepository
        {
            get { return _examenAudicionRepository.Value; }
        }

        public virtual IRepository<ExamenFisico> ExamenFisicoRepository
        {
            get { return _examenFisicoRepository.Value; }
        }

        public virtual IRepository<ExamenPsicologia> ExamenPsicologiaRepository
        {
            get { return _examenPsicologiaRepository.Value; }
        }

        public virtual IRepository<ExamenPsicosensometria> ExamenPsicosensometriaRepository
        {
            get { return _examenPsicosensometriaRepository.Value; }
        }

        public virtual IRepository<ExamenVision> ExamenVisionRepository
        {
            get { return _examenVisionRepository.Value; }
        }

        public virtual IRepository<FactorRiesgo> FactorRiesgoRepository
        {
            get { return _factorRiesgoRepository.Value; }
        }

        public virtual IRepository<HistoriaClinica> HistoriaClinicaRepository
        {
            get { return _historiaClinicaRepository.Value; }
        }

        public virtual IRepository<HistoriaClinicaRegistro> HistoriaClinicaRegistroRepository
        {
            get { return _historiaClinicaRegistroRepository.Value; }
        }

        public virtual IRepository<Individuo> IndividuoRepository
        {
            get { return _individuoRepository.Value; }
        }

        public virtual IRepository<IndividuoBlob> IndividuoBlobRepository
        {
            get { return _individuoBlobRepository.Value; }
        }

        public virtual IRepository<Log> LogRepository
        {
            get { return _logRepository.Value; }
        }

        public virtual IRepository<Medico> MedicoRepository
        {
            get { return _medicoRepository.Value; }
        }

        public UnitOfWork() : this(new HealthBookEntities())
        {
		}

        public UnitOfWork(HealthBookEntities context)
        {
            _context = context;
			_context.ContextOptions.LazyLoadingEnabled = false;

            _anamnesiRepository = GetLazyRepository<Anamnesi>(_context);
            _antecedenteEnfermedadRepository = GetLazyRepository<AntecedenteEnfermedad>(_context);
            _centroTrabajoRepository = GetLazyRepository<CentroTrabajo>(_context);
            _citaRepository = GetLazyRepository<Cita>(_context);
            _examenAudicionRepository = GetLazyRepository<ExamenAudicion>(_context);
            _examenFisicoRepository = GetLazyRepository<ExamenFisico>(_context);
            _examenPsicologiaRepository = GetLazyRepository<ExamenPsicologia>(_context);
            _examenPsicosensometriaRepository = GetLazyRepository<ExamenPsicosensometria>(_context);
            _examenVisionRepository = GetLazyRepository<ExamenVision>(_context);
            _factorRiesgoRepository = GetLazyRepository<FactorRiesgo>(_context);
            _historiaClinicaRepository = GetLazyRepository<HistoriaClinica>(_context);
            _historiaClinicaRegistroRepository = GetLazyRepository<HistoriaClinicaRegistro>(_context);
            _individuoRepository = GetLazyRepository<Individuo>(_context);
            _individuoBlobRepository = GetLazyRepository<IndividuoBlob>(_context);
            _logRepository = GetLazyRepository<Log>(_context);
            _medicoRepository = GetLazyRepository<Medico>(_context);
        }

        private static Lazy<IRepository<TEntity>> GetLazyRepository<TEntity>(ObjectContext context) where TEntity : EntityObject
        {
            return new Lazy<IRepository<TEntity>>(() => new EntityRepository<TEntity>(context));
        }

        public virtual bool Save()
        {
            return _context.SaveChanges() > 0;
        }

        #region IDisposable Methods

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    _context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
	}
}


